package com.zl.easybuy.service.impl;

import com.zl.easybuy.bo.OrderBo;
import com.zl.easybuy.exc.EasyBuyExc;
import com.zl.easybuy.mapper.OrderDetailMapper;
import com.zl.easybuy.mapper.OrderMapper;
import com.zl.easybuy.mapper.ProductMapper;
import com.zl.easybuy.mapper.UserAddressMapper;
import com.zl.easybuy.pojo.Order;
import com.zl.easybuy.pojo.OrderDetail;
import com.zl.easybuy.pojo.Product;
import com.zl.easybuy.pojo.User;
import com.zl.easybuy.service.OrderService;
import com.zl.easybuy.service.ProductService;
import com.zl.easybuy.service.UserAddressService;
import com.zl.easybuy.util.CartItem;
import com.zl.easybuy.util.Pages;
import com.zl.easybuy.util.SessionUtil;
import com.zl.easybuy.util.ShoppingCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    UserAddressMapper userAddressMapper;
    @Autowired
    UserAddressService userAddressService;
    @Autowired
    ProductMapper productMapper;
    @Override
    public Order addOrder(Integer AddressId,String Address, String Remark)  throws EasyBuyExc {
        if(Address.isEmpty()&&Remark.isEmpty()&&AddressId==null){
            throw new EasyBuyExc("地址项不能为空");
        }
        ShoppingCart shoppingCart=SessionUtil.getCurrentCart();
        if(!compare(shoppingCart)){
            throw new EasyBuyExc("库存不足");
        }
        Order order=new Order();
        if(AddressId==-1){
            userAddressService.addAddress(Address,Remark);
            order.setUserAddress(Address);
        }else {
            order.setUserAddress(userAddressMapper.findAddressById(AddressId).getAddress());
        }
        User user=SessionUtil.getCurrentUser();
        if(user==null){
            throw new EasyBuyExc("未登录");
        }
        //设置订单信息
        order.setCost(shoppingCart.getSum());
        order.setCreateTime(new Date());
        order.setLoginName(user.getLoginName());
        order.setSerialNumber(UUID.randomUUID().toString());
        order.setUserId(user.getId());
        //插入订单表
        if(orderMapper.addOrder(order)<=0){
            throw new EasyBuyExc("添加订单失败");
        }
        //订单详情表
        OrderDetail orderDetail=new OrderDetail();
        //遍历购物车中的商品
        for(CartItem cartItem:shoppingCart.getItems()){
            orderDetail.setCost(cartItem.getCost());
            orderDetail.setOrderId(order.getId());
            orderDetail.setQuantity(cartItem.getQuantity());
            orderDetail.setProductId(cartItem.getProduct().getId());
            int ismodify=productMapper.modify(productMapper.findById(cartItem.getProduct().getId()).getStock()-cartItem.getQuantity(),cartItem.getProduct().getId());
            if(orderDetailMapper.addOrderDetail(orderDetail)<=0&&ismodify<=0){
                throw new EasyBuyExc("添加订单失败");
            }
        }
        //清除购物车
        SessionUtil.clearCart();
        return order;
    }

    private boolean compare(ShoppingCart shoppingCart) {
        for(CartItem cartItem:shoppingCart.getItems()){
            Product product =productMapper.findById(cartItem.getProduct().getId());
            if(product.getStock().intValue()<cartItem.getQuantity()){
                return false;
            }
        }
        return true;
    }

    @Override
    public Pages<Order> findOrderById(Integer id, Integer pageNo) {
        Pages<Order> pages=new Pages<>();
        pages.setUrl("admin/order/index?userId="+id);
        pages.setPageNo(pageNo);
        pages.setPageSize(5);
        pages.setTotalCount(orderMapper.countByUserId(id));
        OrderBo orderBo=new OrderBo();
        orderBo.setPage(true);
        orderBo.setPageNo(pageNo);
        orderBo.setPageSize(pages.getPageSize());
        Order order=new Order();
        order.setUserId(id);
        orderBo.setOrder(order);
        pages.setRecord(orderMapper.findOrderBy(orderBo));
        return pages;
    }

    @Override
    public Pages<Order> findAllOrderBy(Integer pageNo) {
        Pages<Order> pages=new Pages<>();
        pages.setUrl("admin/order/queryAllOrder?i=1");
        pages.setPageNo(pageNo);
        pages.setPageSize(5);
        pages.setTotalCount(orderMapper.count());
        OrderBo orderBo=new OrderBo();
        orderBo.setPage(true);
        orderBo.setPageNo(pageNo);
        orderBo.setPageSize(pages.getPageSize());
//        Order order=new Order();
//        orderBo.setOrder(order);
        pages.setRecord(orderMapper.findOrderBy(orderBo));
        return pages;
    }
}
